package com.unc.allergyex;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.unc.allergyex.adapters.DisambiguateAdapter;
import com.unc.allergyex.helpers.ObjManager;
import com.unc.allergyex.objects.db.Restaurant;
import com.unc.allergyex.objects.tasks.MenuQueryTask;

/**
 * This is the activity for the disambiguation screen.  This activity is launched from 
 * the main activity when the user searches for a restaurants and the input string is 
 * not an exact match for exactly one of the restaurants in the list of known restaurants. 
 * The goal of this activity is for the user to be able to select the intended restaurant
 * from a list of possibilities.  The overall layout file is disambiguation.xml,  
 * disambiguationitem.xml is the layout for the list items, and the DisambiguateAdapter is 
 * used as the list adapter.  
 * @author Stephanie
 * @see com.unc.allergyex.adapters.DisambiguateAdapter
 */
public class Disambiguation extends ListActivity{
	
	MenuQueryTask task;

	/**@param savedInstanceState default param for onCreate.  We aren't using this to do anything.  
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.disambiguation);
		ArrayList<Restaurant> rests = new ArrayList<Restaurant>(Arrays.asList(ObjManager.getAmbiguousRestaurantList()));
		DisambiguateAdapter adapter = new DisambiguateAdapter(this, 4, rests);
		setListAdapter(adapter);
	}
	
	/**This method is called when the user taps the forward arrow icon
	 * on the list item.  This method performs the query to get the menu 
	 * for the selected restaurant, which leads to launching of the results activity. 
	 * @param v the view for the selected restaurant
	 */
	public void restaurantSelected(View v){
		View vc = ((ViewGroup)v.getParent()).findViewById(R.id.restaurantIdD); //an invisible textview holds the restaurantId
		String id = ((TextView)vc).getText().toString();
		task = new MenuQueryTask();
		task.setContext(this);
		task.execute(id); //the menuquerytask will take care of the server query and of launching the notification for the user and of launching the results activity with the menu
	}
	
	/**This method is called when the user taps the "Show on Map" button for a particular restaurant.  
	 * This method launches the MapDisambiguation activity for the chosen restaurant.  
	 * @param v
	 */
	public void showOnMapButtonClicked(View v){
		View vc = ((ViewGroup)v.getParent()).findViewById(R.id.restaurantIdD); //an invisible textview holds the restaurantId
		String id = ((TextView)vc).getText().toString();
		
		//get the restaurant from the ObjManager so that we can pass it to the MapsDisambiguation
		Restaurant selectedRestaurant = null;
		for(Restaurant r : ObjManager.getRestaurantList()){
			if(r.getId().equals(id)){
				selectedRestaurant = r;
				break;
			}
		}
		Restaurant[] newR = {selectedRestaurant};
		ObjManager.setMapSearchRestaurantList(newR);
		
		//launch the mapsdisambiguation activity
		Intent intent = new Intent(this, MapDisambiguation.class);
		startActivity(intent);
	}
	
	/**
	 * Causes this activity to close.  This method is called when the back button
	 * is tapped.
	 * @param v default parameter for this method
	 */
	public void returnButtonClicked(View v){
		this.finish();
	}
	
	/**cancel the query task when the activity is paused
	 * @see android.app.Activity#onPause()
	 */
	@Override
	public void onPause(){
		super.onPause();
		cancelTask();
	}
	
	/**cancel the query task when the activity is destroyed
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	public void onDestroy(){
		super.onDestroy();
		cancelTask();
	}
	
	/**cancel the query task if it is not null
	 * 
	 */
	private void cancelTask(){
		if (task != null){
			task.cancel(true);
		}
	}
}
